package com.huanong.controller.impl;

import com.huanong.common.grace.result.GraceJSONResult;
import com.huanong.controller.PersonControllerApi;
import com.huanong.pojo.Person;
import com.huanong.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 *@Author:anran
 *@Date:2020/12/9
 *@Version 1.0
 */

@RestController
public class PersonController implements PersonControllerApi {

    @Autowired
    private PersonService personService;

    @Override
    public GraceJSONResult userLogin(Person person, HttpServletResponse response) {
        Person list=personService.login(person);
        if (list != null){
            Cookie cookie = new Cookie("user", list.getPnId().toString());
            cookie.setPath("/");
            response.addCookie(cookie);
            return GraceJSONResult.ok(list);
        }else {
            return GraceJSONResult.errorMsg("用户登录失败");
        }
    }

    @Override
    public GraceJSONResult userRegister(Person person) {
        int flag=personService.register(person);
        if (flag==1){
            return GraceJSONResult.ok("用户注册成功");
        }else {
            return GraceJSONResult.errorMsg("用户注册失败");
        }
    }

    @Override
    public GraceJSONResult checkUser(String userAccount) {

        boolean flag = personService.checkAccount(userAccount);

        return GraceJSONResult.ok(flag);
    }
}
